package org.bot.http.event;

import com.alibaba.fastjson.JSONObject;
import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.bot.http.client.HttpTool;
import org.springframework.stereotype.Component;

@Component
@EventName("POST请求（站外）")
public class ClientHttpPost implements Events {

    @Override
    public Object exec(FlowNode note, DataStorage data) {
        final Object o = note.getStaticValue() == null ? data.get(note.getDataIndex()) : note.getStaticValue();
        JSONObject param = new JSONObject();
        if (o != null) {
            if (o instanceof String) {
                param = JSONObject.parseObject(o.toString());
            } else if (o instanceof JSONObject) {
                param = (JSONObject) o;
            }
        }
        return HttpTool.doPost(note.getArg(), param);
    }
}
